import java.util.ArrayList;

import processing.core.PConstants;
import controlP5.ControlFont;
import controlP5.ControlP5;
import controlP5.Textfield;
import de.umass.lastfm.Artist;

public class SongView extends View{
	ControlP5 ControlP5;
	
	Textfield TF_Artist, TF_Song;
	controlP5.Button B_Submit;
	
	private String artistInput = "";
	private String songInput = "";
	
	private int midX;
	
	String trackData[];
	String searchResult[];
	ArtistData tempArtist;
	ArtistView artistView1;
	
	private ArrayList<controlP5.Button> Buttons;
	
	private int drawValue = 0;	// 0: draw nothing	 		|| 1: track data
								// 2: track search results 	|| 3: artistview
	
	private boolean fakeArtist = false;
	
	private int currentColor = 0;
	private int cIndex = 0;
	
	public SongView(int _x1, int _y1, int _x2, int _y2) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		
		midX = ((x1+200)+x2)/2;
		
		setup();
	}
	
	public void resize(int _x1, int _y1, int _x2, int _y2) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		
		width = x2 - x1;
		height = y2 - y1;
	}

	private void setup() {
		ControlP5 = new ControlP5(GLOBAL.processing);
			ControlP5.setAutoDraw(false);
			
		TF_Artist = ControlP5.addTextfield("vw_inputArtist", x1+10, y1+40, 200, 20);
			TF_Artist.setCaptionLabel("Artist");
			setFieldColors(TF_Artist);

		TF_Song = ControlP5.addTextfield("vw_inputSong", x1+10, y1+90, 200, 20);
			TF_Song.setCaptionLabel("Song");
			setFieldColors(TF_Song);
			
		B_Submit = ControlP5.addButton("vw_submit", 0, x1+10, y1+145, 100, 25);
			B_Submit.setLabel("Search");
			GLOBAL.setButtonColors(B_Submit);
			
		Buttons = new ArrayList<controlP5.Button>();
		
		int yOffset=85;
		for(int x=0; x<10; ++x) {
			controlP5.Button temp = ControlP5.addButton("vw_btn:"+x, 0, x1+235, y1+yOffset, 25, 25);
			GLOBAL.setButtonColors(temp);
			temp.setLabel("Go");
			temp.setVisible(false);
			Buttons.add(temp);
			yOffset += 35;
		}
	}
	
	public void draw() {
		ControlP5.draw();
		GLOBAL.processing.fill(0);
		GLOBAL.processing.textFont(GLOBAL.fontSegoe, 12);
		GLOBAL.processing.textAlign(PConstants.LEFT);
		GLOBAL.processing.text("Search For a Song and/or Artist", 18, 74);
		GLOBAL.processing.text("Warning! Speed dependent", 5, 250);
		GLOBAL.processing.text("on Last.fm traffic.", 15, 264);
		
		//NO RESULTS
		if(drawValue == 0) {
			GLOBAL.processing.fill(currentColor);
			GLOBAL.processing.textAlign(PConstants.CENTER);
			GLOBAL.processing.textSize(20);
			GLOBAL.processing.text("No Results.", midX, y1+100);
		}
		
		//TRACK INFO
		else if(drawValue == 1) {
			GLOBAL.processing.fill(0);
			GLOBAL.processing.textAlign(PConstants.CENTER);
			
			GLOBAL.processing.textSize(18);
			GLOBAL.processing.text(trackData[0], midX, y1+30);
			
			GLOBAL.processing.textSize(14);
			GLOBAL.processing.text("By: " + trackData[1], midX, y1+50);
			
			GLOBAL.processing.textSize(12);
			GLOBAL.processing.text(removeHTML(trackData[7]), midX, y1+90);
			
			GLOBAL.processing.textAlign(PConstants.LEFT);
			GLOBAL.processing.text("Album:\t" + trackData[2], x1+225, y1+260);
			GLOBAL.processing.text("Duration:\t" + trackData[3], x1+225, y1+280);
			GLOBAL.processing.text("Play Count:\t" + trackData[6], x1+225, y1+300);
			GLOBAL.processing.text("Song URL:\t" + trackData[4], x1+225, y1+350);
			GLOBAL.processing.text("Artist URL:\t" + trackData[5], x1+225, y1+370);
			
			//GLOBAL.processing.link("http://processing.org", "_new"); 
			
		}
		
		//SEARCH RESULTS
		else if(drawValue == 2) {
			int yOffset = 100;
			for(int x=0; x<20; x+=2) {
				GLOBAL.processing.text(searchResult[x], x1+275, y1+yOffset);
				GLOBAL.processing.text( "--   " + searchResult[x+1], x1+500, y1+yOffset);
				yOffset += 35;
				Buttons.get(x/2).show();
			}
			
			
		}
		
		//ARTIST VIEW
		else if(drawValue == 3) {
			artistView1.draw();
			
			if(fakeArtist) {
				GLOBAL.processing.fill(GLOBAL.colorBackground);
				GLOBAL.processing.rect(250, 500, 900, 768);
			}
		}
	}
	
	private void hideButtons() {
		for(int x=0; x<Buttons.size(); ++x) {
			Buttons.get(x).hide();
		}
	}
	
	public String getArtistInput() {
		return artistInput;
	}
	
	public String getSongInput() {
		return songInput;
	}
	
	public void handleEvent(String event) {
		if(event.equals("vw_submit")) {
			artistInput = TF_Artist.getText();
			songInput = TF_Song.getText();
			search();
		}
		else if(event.contains("btn:")) {
			int index = event.indexOf(":") + 1;
			int num = Integer.parseInt(event.substring(index));
			
			songInput = searchResult[num];
			artistInput = searchResult[num+1];
			
			search();
		}
	}
	
	private void setFieldColors(Textfield tf) {
		tf.setColorBackground(GLOBAL.colorTabSelected);
		tf.setColorActive(0);
		tf.setColorForeground(GLOBAL.colorTabDeselected);
		tf.setColorValueLabel(0);
		tf.setColorCaptionLabel(0);
		tf.captionLabel().setControlFont(new ControlFont(GLOBAL.fontSegoe));
		tf.captionLabel().setControlFontSize(10);
	}
	
	private void search() {
		hideButtons();
		tempArtist = null;
		fakeArtist = false;
		drawValue = 0;
		
		if(songInput.equals("") && artistInput.equals("")) {
			drawValue = 0;
			currentColor = getNextStringColor();
			return;
		}
		
		//Search Artist Only
		if(songInput.equals("")){
			Artist a = LastfmAPI.searchArtist(artistInput);
			
			int index = GLOBAL.findArtist(a.getName()); 
			if(index > -1) {
				tempArtist = radioGaGa.ArtistList.get(index);
			}
			else {
				tempArtist = new ArtistData(a.getId());
				tempArtist.totalCount = a.getListeners();
				tempArtist.setArtistName(a.getName());
				tempArtist.setupBreakdown();
				tempArtist.theBreakdown.artistID = a.getId();
				fakeArtist = true;
			}
			artistView1 = new ArtistView(x1+300, y1+100, 880, 600, tempArtist);
			drawValue = 3;
		}
		
		//Search for Song Only
		else if(artistInput.equals("")) {
			searchResult = LastfmAPI.searchTrack(songInput);
			
			if(searchResult == null || searchResult[0] == null) {
				drawValue = 0;
				currentColor = getNextStringColor();
				return;
			}
			
			drawValue = 2;
		}
		
		//Search for Song & Artist
		else {
			trackData = LastfmAPI.searchArtistAndTrack(artistInput, songInput);
			
			if(trackData[0] == null || trackData == null) {
				drawValue = 0;
				currentColor = getNextStringColor();
				return;
			}
			drawValue = 1;
			removeDataNulls();
		}
	}
	
	private void removeDataNulls() {
		for(int x=0; x<trackData.length; ++x){
			if(trackData[x] == null){
				trackData[x] = "";
			}
		}
		
		trackData[7] = breakString(trackData[7]);
	}
	
	private String breakString(String text) {
		if(text.length() < 20)
			return text;
		
		int maxLength = 70;
		int index1 = text.indexOf(" ", maxLength);
		
		while(index1 > -1) {
			text = text.substring(0, index1) + "\n" + text.substring(index1+1);
			maxLength += 70;
			index1 = text.indexOf(" ", maxLength);
		}
		
		text = text.replaceAll("&amp;", "&");
		text = text.replaceAll("&quot;", "\"");
		text = text.replaceAll("&uacute;", "u");
		
		return text;
	}

	private int getNextStringColor() {
		if(cIndex == 4) { cIndex = 0; }
		
		switch (cIndex) {
		case 0: cIndex++; return 0;
		case 1: cIndex++; return GLOBAL.colorTabDeselected;
		case 2: cIndex++; return GLOBAL.colorEllipse;
		case 3: cIndex++; return GLOBAL.colorTabMouseOver;
		}
		
		return 0;
	}
	
	private String removeHTML(String text){
		int index1 = text.indexOf("<");
		int index2 = text.indexOf(">");
		
		while(index1 > -1) {
			if(index2 == text.length()-1)
				index2--;
				
			text = text.substring(0, index1) + text.substring(index2+1);
			index1 = text.indexOf("<");
			index2 = text.indexOf(">");
		}
		
		return text;
	}
	
}

